﻿using UnityEngine;
using System.Collections;

public class hiddenPlatformScript : MonoBehaviour {
	
	public hiddenPlatButton motherButton;
	PlatformCollision myPCol;
	bool isFading;
	bool isFadingIn;
	float fadeSpeed = 0.5f;
	Renderer myRend;
	public bool isStepping;
	
	public Shader otherWorldShader;
	
	void Awake()
	{
		myRend = transform.GetChild(0).renderer;
		if(motherButton != null)
			motherButton.platforms.Add(this);
		//myPCol = GetComponent<PlatformCollision>();
		Color tmpColor = myRend.material.color;
		tmpColor.a = 0;
		myRend.material.color = tmpColor;
		setColliderOffSet(1);
	}
	
	void Start()
	{
		this.collider.enabled = false;
		if(networkView.isMine)
			myRend.material.shader = otherWorldShader;
	}
	
	public void setColliderOffSet(int world)
	{
		Vector3 tmpPos = transform.position;
		if(world == 2)
			tmpPos.z = 3;
		else
			tmpPos.z = 0;
		transform.position = tmpPos;
		//Debug.Log(transform.position);
	}
	
	// Update is called once per frame
	void Update () {
	
		if(isFading)
		{
			Color tmpColor = myRend.material.color;
			float fadeAmount = Time.deltaTime * fadeSpeed;
			if(isFadingIn)
			{
				tmpColor.a += fadeAmount;
				if(tmpColor.a >= 1)
				{
					//isFading = false;
					tmpColor.a = 1;
					//myPCol.isIgnore = false;
				}
			}
			else{
				tmpColor.a -= fadeAmount;
				if(tmpColor.a <= 0)
				{
					isFading = false;
					tmpColor.a = 0;
					//myPCol.isIgnore = true;
				}
			}
			myRend.material.color = tmpColor;
		}
		else
		{
			Color tmpColor = myRend.material.color;
			float fadeAmount = Time.deltaTime * fadeSpeed;
			
			if(isStepping && !networkView.isMine)
			{
				if(tmpColor.a < 1.0f)
					tmpColor.a += fadeAmount * 3;
				//Debug.Log("Bef: " + tmpColor.a);
			}
			else if(tmpColor.a > 0)
				tmpColor.a -= fadeAmount * 3;
			//Debug.Log(tmpColor.a);
			myRend.material.color = tmpColor;
		}
		if(isStepping)
			isStepping = false;
	}
	
	public void FadeIn()
	{
		isFading = true;
		isFadingIn = true;
	}
	
	public void FadeOut()
	{
		isFading = true;
		isFadingIn = false;
	}
}
